<template>
  <div class="stars">
    <div class="star">
      <span
        class="star-item"
        v-for="(itemClass,index) in 
     itemClasses"
        :key="index"
        :class="itemClass"
      ></span>
    </div>
  </div>
</template>

<script>
const LENGTH = 5;
const CLS_ON = "on";
const CLS_HALF = "half";
const CLS_OFF = "off";

export default {
  props: {
    score: String,
  },
  data() {
    return {};
  },
  computed: {
    itemClasses() {
      let result = [];
      let score = Math.floor(this.score * 2) / 2;
      let hasDecimal = this.score % 1 !== 0;

      let integer = Math.floor(score);

      for (let i = 0; i < integer; i++) {
        result.push(CLS_ON);
      }

      // 处理半星
      if (hasDecimal) {
        result.push(CLS_HALF);
      }

      // 补齐
      while (result.length < LENGTH) {
        result.push(CLS_OFF);
      }

      return result;
    },
  },
};
</script>

<style>
.stars {
  position: absolute;
  top: 75px;
}
.star {
  font-size: 0;
}
.star .star-item {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 3px;
  background-repeat: no-repeat;
  background-size: 10px 10px;
}
.star .star-item:last-child {
  margin-right: 0;
}
.star .on {
  background-image: url(./image/star24_on@2x.png);
}
.star .half {
  background-image: url(./image/star24_half@2x.png);
}
.star .off {
  background-image: url(./image/star24_off@2x.png);
}
</style>

